﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace TP1___Gestion_Clavier
{
	public partial class Geek : UserControl
	{
		// Position du personnage
		public int X { get; set; }
        public int Y { get; set; }
		public Canvas cnvPere { get; set; }
		public enum Direction { droite, haut, bas, gauche,haut_gauche, haut_droite, bas_gauche, bas_droite };
		public Direction dir {get;set;}
		public Geek()
		{
			// Requis pour initialiser des variables
			InitializeComponent();
			// Init de la position
			this.X = 250;
    		this.Y = 400;
			this.dir = Direction.haut;
		}
		public void Draw(){
			// Affichage dans le bon sens
			RotateTransform rotation = new RotateTransform();
			if (this.dir == Direction.haut) { rotation.Angle = 0; }
			if (this.dir == Direction.droite) { rotation.Angle = 90; }
			if (this.dir == Direction.gauche) { rotation.Angle = 270; }
			if (this.dir == Direction.bas) { rotation.Angle = 180; }
			if (this.dir == Direction.haut_gauche) { rotation.Angle = 315; }
			if (this.dir == Direction.haut_droite) { rotation.Angle = 45; }
			if (this.dir == Direction.bas_gauche) { rotation.Angle = 225; }
			if (this.dir == Direction.bas_droite) { rotation.Angle = 135; }
			
			rotation.CenterX = 25;
			rotation.CenterY = 25;
			this.RenderTransform = rotation;
			
			Canvas.SetLeft(this, this.X);
			Canvas.SetTop(this, this.Y);
		}
		private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// Affichage au bon endroit
			Canvas.SetLeft(this, this.X);
			Canvas.SetTop(this, this.Y);
		}
		public void MiseAJour(MainPage mp)
		{
			int new_x = this.X;
			int new_y = this.Y;
			// Déplacement haut / bas
			if (EtatClavier.ToucheEnfoncee(Key.Up))
			{
				this.dir = Direction.haut;
				new_y -= 50;
			}
			if (EtatClavier.ToucheEnfoncee(Key.Down))
			{
				this.dir = Direction.bas;
				new_y += 50;
			}
			// Déplacement gauche / droite
			if (EtatClavier.ToucheEnfoncee(Key.Left))
			{
				new_x -= 50;
				if (EtatClavier.ToucheEnfoncee(Key.Up)) {
					this.dir=Direction.haut_gauche;
				}
				else if(EtatClavier.ToucheEnfoncee(Key.Down)) {
					this.dir=Direction.bas_gauche;
				}
				else {
					this.dir = Direction.gauche;
				}
			}
			 if (EtatClavier.ToucheEnfoncee(Key.Right))
			{
				new_x += 50;
				if (EtatClavier.ToucheEnfoncee(Key.Up)) {
					this.dir=Direction.haut_droite;
				}
				else if(EtatClavier.ToucheEnfoncee(Key.Down)) {
					this.dir=Direction.bas_droite;
				}
				else {
					this.dir = Direction.droite;
				}
			}
			// Vérification du déplacement
			if (mp.Zone_OK(new_x, new_y, (int)this.rect.Width, (int)this.rect.Height, this.dir)) {
				this.X = new_x;
				this.Y = new_y;
			}
			// Affichage à la nouvelle position
			this.Draw();
		}
	}
}